package org.shiro.service.Impl;

import org.shiro.dao.PermissionMapper;
import org.shiro.entity.Permission;
import org.shiro.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

/** @author HKC */
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
@Service
public class PermissionServiceImpl implements PermissionService {
  @Autowired private PermissionMapper permissionMapper;

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  @Override
  public int create(Permission permission) {
    Assert.notNull(permission, "参数错误");
    int res = permissionMapper.insertSelective(permission);
    if (res > 0) {
      return res;
    }
    return 0;
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  @Override
  public int remove(Long id) {
    Assert.notNull(id, "参数错误");
    int res = permissionMapper.deleteByPrimaryKey(id);
    if (res > 0) {
      return res;
    }
    return 0;
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  @Override
  public Permission load(Long id) {
    Assert.notNull(id, "参数错误");
    Permission menu = permissionMapper.selectByPrimaryKey(id);
    if (menu != null) {
      return menu;
    }
    return null;
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  @Override
  public int modity(Permission permission) {
    Assert.notNull(permission, "参数错误");
    int res = permissionMapper.updateByPrimaryKeySelective(permission);
    if (res > 0) {
      return res;
    }
    return 0;
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  @Override
  public List<Permission> queryAll() {
    List<Permission> permissionList = permissionMapper.selectAll();
    if (permissionList != null && permissionList.size() > 0) {
      return permissionList;
    }
    return null;
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  @Override
  public List<Permission> queryByTree() {
    List<Permission> permissionList = permissionMapper.queryByTree();
    for (Permission permission : permissionList) {
      add(permission);
    }
    return permissionList;
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  @Override
  public List<Permission> queryMenuByRoleId(Long id) {
    Assert.notNull(id, "参数错误");
    List<Permission> permissionList = permissionMapper.queryMenuByRoleId(id);
    if (permissionList.isEmpty() || permissionList.size() <= 0) {
      throw new IllegalArgumentException("查询结果为空，请先添加数据");
    }
    return permissionList;
  }

  public void add(Permission permission) {
    List<Permission> permissionList = permissionMapper.getChildByParentId(permission.getId());
    for (Permission temp : permissionList) {
      if (!temp.getLevel()) {
        add(temp);
      }
    }
    permission.getChild().addAll(permissionList);
  }
}
